import axios from 'axios'
import URL from './url'

export default function (url, json, method = 'post', timeout = 25000) {
  return new Promise((resolve, reject) => {
    if (!url || !json) {
      reject(new Error({status: 0, msg: `url or josn is null`}))
    }

    let params = {
      url: URL.API + url,
      method: method,
      data: json,
      timeout: timeout,
      headers: {'Accept': 'application/json', 'Content-Type': 'application/json'}
    }
    axios(params)
      .then((response) => {
        resolve(response.data)
      })
      .catch((error) => {
        console.error(`ajax error: ${url} ### ${error}`)
        if (error.message) {
          reject(new Error(error.message))
        } else {
          reject(new Error(`ajax 异常: ${url}`))
        }
      })
  })
}
